﻿
    using Chain.BLL;
    using Chain.Model;
    using System;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public partial class ModuleManage : Page
    {

        private Chain.BLL.SysGroupAuthority sysGroupAuthorityBll = new Chain.BLL.SysGroupAuthority();
 

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (this.Page.IsValid)
            {
                try
                {
                    Chain.Model.SysModule sysModuleModel = new Chain.Model.SysModule();
                    sysModuleModel.ModuleCaption = this.txtModuleCaption.Text.Trim();
                    sysModuleModel.ModuleLink = this.txtModuleLink.Text.Trim();
                    sysModuleModel.ModuleParentID = (base.Request["ModuleParentID"] == null) ? 1 : int.Parse(base.Request["ModuleParentID"]);
                    sysModuleModel.ModuleOrder = Convert.ToInt32(this.txtModuleOrder.Text.Trim());
                    sysModuleModel.ModuleVisible = this.ckbModuleVisible.Checked;
                    sysModuleModel.ModuleIcoPath = this.txtModuleIcoPath.Text.Trim();
                    sysModuleModel.ModuleRemark = "";
                    Chain.BLL.SysModule sysModuleBll = new Chain.BLL.SysModule();
                    int flag = sysModuleBll.Add(sysModuleModel);
                    sysModuleModel.ModuleID = flag;
                    sysModuleModel.ModuleLink = sysModuleModel.ModuleLink + "?PID=" + flag;
                    sysModuleBll.Update(sysModuleModel);
                    base.ClientScript.RegisterClientScriptBlock(base.GetType(), Guid.NewGuid().ToString(), "<script>alert('栏目模块添加成功')</script>");
                    this.txtModuleCaption.Text = this.txtModuleLink.Text = this.txtModuleOrder.Text = string.Empty;
                    this.ckbModuleVisible.Checked = false;
                    this.txtModuleIcoPath.Text = "images/ico/";
                    Chain.BLL.SysModuleAction sysModuleActionBll = new Chain.BLL.SysModuleAction();
                    Chain.Model.SysModuleAction sysModuleActionModel = new Chain.Model.SysModuleAction();
                    sysModuleActionModel.ActionCaption = "访问";
                    sysModuleActionModel.ActionControl = "page";
                    sysModuleActionModel.ActionRemark = "页面访问";
                    sysModuleActionModel.ActionModuleID = new int?(flag);
                    flag = sysModuleActionBll.Add(sysModuleActionModel);
                    Chain.Model.SysGroupAuthority sysGroupAuthorityModel = new Chain.Model.SysGroupAuthority();
                    sysGroupAuthorityModel.GroupID = 1;
                    sysGroupAuthorityModel.ModuleID = sysModuleActionModel.ActionModuleID;
                    sysGroupAuthorityModel.ActionID = new int?(flag);
                    sysGroupAuthorityModel.ActionValue = true;
                    flag = this.sysGroupAuthorityBll.Add(sysGroupAuthorityModel);
                }
                catch (Exception ex)
                {
                    base.ClientScript.RegisterClientScriptBlock(base.GetType(), Guid.NewGuid().ToString(), "<script>alert('栏目模块添加失败：" + ex.Message + "')</script>");
                }
                this.gvwModuleMamage.DataBind();
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            base.Response.Redirect("ColumnManage.aspx");
        }

        protected void ckbIsPowerReg_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox ckbIsPowerReg = (CheckBox) sender;
            bool isPowerReg = ckbIsPowerReg.Checked;
            int moduleID = Convert.ToInt32(ckbIsPowerReg.Attributes["ModuleID"]);
            int actionID = Convert.ToInt32(ckbIsPowerReg.Attributes["ActionID"]);
            Chain.Model.SysGroupAuthority sysGroupAuthorityModel = new Chain.Model.SysGroupAuthority();
            sysGroupAuthorityModel.GroupID = 1;
            sysGroupAuthorityModel.ModuleID = new int?(moduleID);
            sysGroupAuthorityModel.ActionID = new int?(actionID);
            sysGroupAuthorityModel.ActionValue = isPowerReg;
            this.sysGroupAuthorityBll.UpdateOrEndowPower(sysGroupAuthorityModel);
        }

        protected void gvwModuleMamage_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Chain.Model.SysModule sysModuleModel = (Chain.Model.SysModule) e.Row.DataItem;
                Chain.Model.SysModuleAction sysModuleActionModel = new Chain.BLL.SysModuleAction().GetModelByModuleIDAndControl(sysModuleModel.ModuleID, "page");
                if (sysModuleActionModel != null)
                {
                    bool isPowerReg = new Chain.BLL.SysGroupAuthority().isPowerReg(1, sysModuleActionModel.ActionModuleID.Value, sysModuleActionModel.ActionID);
                    CheckBox ckbIsPowerReg = (CheckBox) e.Row.FindControl("ckbIsPowerReg");
                    ckbIsPowerReg.Checked = isPowerReg;
                    ckbIsPowerReg.Attributes.Add("ModuleID", sysModuleActionModel.ActionModuleID.Value.ToString());
                    ckbIsPowerReg.Attributes.Add("ActionID", sysModuleActionModel.ActionID.ToString());
                }
            }
        }

        protected void odsModuleMamage_Updated(object sender, ObjectDataSourceStatusEventArgs e)
        {
            if (e.ExceptionHandled)
            {
                base.ClientScript.RegisterClientScriptBlock(base.GetType(), Guid.NewGuid().ToString(), "<script>alert('栏目模块修改失败：" + e.Exception.Message + "')</script>");
                e.ExceptionHandled = false;
            }
        }

        protected void odsModuleMamage_Updating(object sender, ObjectDataSourceMethodEventArgs e)
        {
            Chain.Model.SysModule sysModuleModel = (Chain.Model.SysModule) e.InputParameters[0];
            sysModuleModel.ModuleRemark = "";
            if ((sysModuleModel.ModuleCaption == null) || (sysModuleModel.ModuleCaption.Trim() == ""))
            {
                base.ClientScript.RegisterClientScriptBlock(base.GetType(), Guid.NewGuid().ToString(), "<script>alert('模块菜单名称不能为空!')</script>");
                e.Cancel = true;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }
    }


